package com.bkgy.modular.tpdodata_cal1630.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tpdodata_cal1630.entity.*;
import com.bkgy.modular.tpdodata_cal1630.mapper.TBTensionDataCal1630Mapper;
import com.bkgy.modular.tpdodata_cal1630.mapper.TPdoDataCal1630FlatnessBasicMapper;
import com.bkgy.modular.tpdodata_cal1630.mapper.TPdoDataCal1630Mapper;
import com.bkgy.modular.tpdodata_cal1630.mapper.TPdoDataCalBao1630Mapper;
import com.bkgy.modular.tpdodata_cal1630.service.TPdoDataCal1630Service;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
* @author dell
* @description 针对表【T_PDO_DATA_CAL1630(2150连退生产计划表)】的数据库操作Service实现
* @createDate 2025-04-09 11:11:14
*/
@Service
public class TPdoDataCal1630ServiceImpl extends ServiceImpl<TPdoDataCal1630Mapper, TPdoDataCal1630>
    implements TPdoDataCal1630Service{

    @Autowired
    private TPdoDataCalBao1630Mapper tPdoDataCalBao1630Mapper;

    @Autowired
    private TPdoDataCal1630FlatnessBasicMapper tPdoDataCal1630FlatnessBasicMapper;

    @Autowired
    private TBTensionDataCal1630Mapper tbTensionDataCal1630Mapper;

    /**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataByPage(String data) {
        try {
            TPdoData1630Request tPdoData1630Request = JSON.parseObject(data, TPdoData1630Request.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tPdoData1630Request.getPageIndex();
            Integer pageSize = tPdoData1630Request.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if(null!=tPdoData1630Request.getCondition()){
                jsonObject  = JSON.parseObject(tPdoData1630Request.getCondition().toString());
            }

            QueryWrapper<TPdoDataCalBao1630> queryWrapper = new QueryWrapper<TPdoDataCalBao1630>();
            queryWrapper.orderByDesc("PROD_START_TIME");


            Page<TPdoDataCalBao1630> page = new Page<TPdoDataCalBao1630>(pageIndex, pageSize);
            IPage<TPdoDataCalBao1630> list = tPdoDataCalBao1630Mapper.selectPage(page, queryWrapper);

            return ResultData.ResultDataSuccess(list,(int)list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }


    /**
     * 分页查看历史卷轧制力，弯辊力AI设定值
     * @param data 分页参数字符串
     */
    public ResultData findHistorySetValueByPage(String data) {
        try {
            TPdoDataCal1630FlatnessBasicReq tPdoData1630Request = JSON.parseObject(data, TPdoDataCal1630FlatnessBasicReq.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tPdoData1630Request.getPageIndex();
            Integer pageSize = tPdoData1630Request.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            tPdoData1630Request.setIfWeild(new BigDecimal(1));
            Page<TPdoDataCal1630FlatnessBasic> page = new Page<TPdoDataCal1630FlatnessBasic>(pageIndex, pageSize);

            IPage<TPdoDataCal1630FlatnessBasicResp> list = tPdoDataCal1630FlatnessBasicMapper.getHistorySetValue(page,tPdoData1630Request);
//            for(int i=0;i<list.getSize();i++){
//                String entid=list.getRecords().get(i).getEntid();
//                FlatnessWarn flatnessWarn=flatnessWarnMapper.getFlatnessWarnByEntid(entid);
//                if(flatnessWarn!=null){
//                    list.getRecords().get(i).setRollingForceSet(flatnessWarn.getRollingForce());
//                }
//            }

            return ResultData.ResultDataSuccess(list,(int) list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    public ResultData findHistoryFlatnessPercentage(String data){
        TPdoDataCal1630FlatnessPercentageRsp tPdoDataCal1630FlatnessPercentageRsp=tPdoDataCal1630FlatnessBasicMapper.findHistoryFlatnessPercentage();
        return ResultData.ResultDataSuccess(tPdoDataCal1630FlatnessPercentageRsp);

    }

    public ResultData findTensionPercentage(JSONObject jsonObject){
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<TBTensionDataCal1630PercentageRsp> list=tbTensionDataCal1630Mapper.findTensionPercentage(condition);
        return ResultData.ResultDataSuccess(list);

    }

    public ResultData findTensionData(String data){

        TPdoData1630Request tPdoData1630Request = JSON.parseObject(data, TPdoData1630Request.class);
        JSONObject jsonObject  = null;
        Integer pageIndex = tPdoData1630Request.getPageIndex();
        Integer pageSize = tPdoData1630Request.getPageSize();

        if(null == pageIndex || null == pageSize){
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        QueryWrapper<TBTensionDataCal1630> queryWrapper = new QueryWrapper<TBTensionDataCal1630>();

        if(null!=tPdoData1630Request.getCondition()){
            jsonObject  = JSON.parseObject(tPdoData1630Request.getCondition().toString());
        }
        String dbegin = jsonObject.getString("dbegin"); // 开始时间
        String dend = jsonObject.getString("dend"); // 结束时间

        String steelgrade = jsonObject.getString("steelgrade"); // 钢种
        String thickmin = jsonObject.getString("thickmin"); // 厚度最小值
        String thickmax = jsonObject.getString("thickmax"); // 厚度最大值

        String widthmin = jsonObject.getString("widthmin"); // 宽度最小值
        String widthmax = jsonObject.getString("widthmax"); // 宽度最大值

        if (!jsonObject.isEmpty()) {

            if (StringUtils.hasText(steelgrade)) {
                queryWrapper.eq("STEELGRADE",steelgrade);
            }
            // 判断开始时间参数是否存在,存在进入判断添加条件
            if (StringUtils.hasText(dbegin) && StringUtils.hasText(dend)) {
                // getProcess_end_time大于开始时间
                queryWrapper.ge("CREATE_TIME",dbegin);
                // getProcess_end_time小于于结束时间
                queryWrapper.le("CREATE_TIME",dend);

            }

            if (StringUtils.hasText(thickmin) && StringUtils.hasText(thickmax)) {
                // getProcess_end_time大于开始时间
                queryWrapper.ge("OUT_MAT_THK",thickmin);
                // getProcess_end_time小于于结束时间
                queryWrapper.le("OUT_MAT_THK",thickmax);

            }

            if (StringUtils.hasText(widthmin) && StringUtils.hasText(widthmax)) {
                // getProcess_end_time大于开始时间
                queryWrapper.ge("OUT_MAT_WIDTH",widthmin);
                // getProcess_end_time小于于结束时间
                queryWrapper.le("OUT_MAT_WIDTH",widthmax);

            }

        }
        queryWrapper.orderByDesc("case when CREATE_TIME IS NULL then 0 else 1 end").orderByDesc("CREATE_TIME");
        Page<TBTensionDataCal1630> page = new Page<TBTensionDataCal1630>(pageIndex, pageSize);
        IPage<TBTensionDataCal1630> list = tbTensionDataCal1630Mapper.selectPage(page,queryWrapper);
        return ResultData.ResultDataSuccess(list);
    }

}




